package estatistico.presenter;

import estatistico.view.ManterNumerosView;
import java.awt.AWTKeyStroke;
import java.awt.KeyboardFocusManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.HashSet;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

public final class ManterNumerosPresenter {

    private ManterNumerosView view;
    private DefaultListModel numerosModel;
    private ArrayList<Double> numeros;

    public ManterNumerosPresenter(ArrayList<Double> numeros) {
        this.numeros = numeros;
        numerosModel = new DefaultListModel();
        for (Double numero : numeros) {
            numerosModel.addElement(numero);
        }
        
        view = new ManterNumerosView(null);

        HashSet conj = new HashSet(view.getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS));
        conj.add(AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_ENTER, 0));
        view.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, conj);


        view.getLstNumeros().setModel(numerosModel);


        view.getBtnInserir().addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    incluir();
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(view, "Falha: " + ex.getMessage());
                }
            }
        });

        view.getBtnRemover().addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    remover();
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(view, "Falha: " + ex.getMessage());
                }
            }
        });

        view.getBtnConfirmar().addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    confirmar();
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(view, "Falha: " + ex.getMessage());
                }
            }
        });


        view.setVisible(true);
    }

    private void incluir() throws Exception {
        String strNumero = view.getTxtNumero().getValue().toString();
        if (strNumero == null || strNumero.equals("")) {
            throw new Exception("Informe um número válido");
        }
        numerosModel.addElement(strNumero);
        view.getTxtNumero().setText("");
        view.getLstNumeros().setModel(numerosModel);
        view.getTxtNumero().requestFocus();
    }

    private void remover() throws Exception {
        int posSelecionado = view.getLstNumeros().getSelectedIndex();
        if (posSelecionado < 0) {
            throw new Exception("É necessário selecionar um número que deseja remover.");
        }
        numerosModel = (DefaultListModel) view.getLstNumeros().getModel();
        numerosModel.removeElementAt(view.getLstNumeros().getSelectedIndex());
        numeros.clear();
        for (int i = 0; i < numerosModel.getSize(); i++) {
            numeros.add(Double.parseDouble((String) numerosModel.getElementAt(i).toString()));
        }
        view.getLstNumeros().setModel(numerosModel);
    }

    private void confirmar() throws Exception {
        try {
            if (view.getLstNumeros().getModel().getSize() > 0) {
                numeros.clear();
                numerosModel = (DefaultListModel) view.getLstNumeros().getModel();
                for (int i = 0; i < numerosModel.getSize(); i++) {
                    numeros.add(Double.parseDouble((String) numerosModel.getElementAt(i).toString()));
                }
            }
        } catch (Exception e) {
            throw new Exception("Problema ao retornar os números");
        } finally {
            view.setVisible(false);
        }
    }

    public ArrayList<Double> getNumeros() {
        return numeros;
    }

    public void dispose() {
        view.dispose();
    }
}
